//
//  FriendViewController.m
//  V-Learn
//
//  Created by Apple on 2018/12/20.
//  Copyright © 2018 yxk. All rights reserved.
//

#import "FriendViewController.h"
#import "LBScrollView.h"
#import "LBHotVC.h"
#import "LBFollowVC.h"
@interface FriendViewController ()<seletedControllerDelegate,UIScrollViewDelegate>{
    LBScrollView *titleScroll;
    UIScrollView *mainScroll;
    LBFollowVC *followVC;
    LBHotVC *hotVC;
}


@end

@implementation FriendViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.canScroll = YES;
    
//    if (@available(iOS 11.0, *)) {
//        titleScroll.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//        mainScroll.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//    } else {
//        self.automaticallyAdjustsScrollViewInsets = NO;
//
//    }
    //创建头部视图(滑动视图)
    titleScroll= [[LBScrollView alloc] initWithFrame:CGRectMake(100*ScaleX, 0, SCREEN_WIDTH-100*ScaleX, LBNAVBAR_HEIGHT)];
    
    NSArray *mu = @[@"关注",@"热门"];
    
    titleScroll.headArray = mu.mutableCopy;
    
    //设置代理
    titleScroll.SeletedDelegate = self;
    titleScroll.directionalLockEnabled = YES;
    titleScroll.alwaysBounceVertical = YES;
    
    mainScroll.directionalLockEnabled = YES;
    mainScroll.alwaysBounceVertical = YES;
//    titleScroll.backgroundColor = [UIColor redColor];
    //添加
    [self.view addSubview:titleScroll];
    
    //创建一个滑动视图用来滑动Viewcontroller
    mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, LBNAVBAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-LBNAVBAR_HEIGHT)];
//    self.automaticallyAdjustsScrollViewInsets= NO;
    //设置代理
    mainScroll.delegate = self;
    //设置滑动区域
    mainScroll.contentSize = CGSizeMake(SCREEN_WIDTH*mu.count, 0) ;
    //打开分页功能
    mainScroll.pagingEnabled = YES;
    //设置背景颜色
    mainScroll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:mainScroll];
    //设置当前子控制器
    followVC = [[LBFollowVC alloc] init];
    [self addChildViewController:followVC];
    followVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-LBNAVBAR_HEIGHT);
    //将当前的子视图控制器的view添加到主滑动视图上
    [mainScroll addSubview:followVC.view];
    hotVC = [[LBHotVC alloc] init];
    [self addChildViewController:hotVC];
    hotVC.view.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT-LBNAVBAR_HEIGHT);
    [mainScroll addSubview:hotVC.view];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (!_canScroll) {
        CGPoint tPoint = scrollView.contentOffset;
        tPoint.x = 0;
        scrollView.contentOffset = tPoint;
    }
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//    if (mainScroll.contentOffset.x < SCREEN_WIDTH) {
//        hotVC = [[LBHotVC alloc] init];
//        [self addChildViewController:hotVC];
//        hotVC.view.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
//        [mainScroll addSubview:hotVC.view];
//    }
    [titleScroll changeBtntitleColorWith:scrollView.contentOffset.x/SCREEN_WIDTH+1000];
//    NSLog(@"%f",scrollView.contentOffset.x/SCREEN_WIDTH+1000);
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    NSLog(@"%f",scrollView.contentOffset.x/SCREEN_WIDTH+1000);
    [titleScroll changeBtntitleColorWith:scrollView.contentOffset.x/SCREEN_WIDTH+1000];
    
}

#pragma mark -头部scrollView的代理方法的实现
-(void)seletedControllerWith:(UIButton *)btn{
//    NSLog(@"%ld",btn.tag-1000);
    NSString *pageStr = [NSString stringWithFormat:@"%ld",btn.tag-1000];
    mainScroll.contentOffset = CGPointMake(SCREEN_WIDTH*(btn.tag - 1000), 0);
    [titleScroll changeBtntitleColorWith:mainScroll.contentOffset.x/SCREEN_WIDTH+1000];

    if ([pageStr isEqualToString:@"0"]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"ShequanFollow" object:@{@"pageStr":pageStr} userInfo:nil];
    }else{
            [[NSNotificationCenter defaultCenter] postNotificationName:@"ShequanHot" object:@{@"pageStr":pageStr} userInfo:nil];
    }
}


@end
